<?php

class DateControl extends ValueControl {

	private $allow_null = false;
	public function WithAllowNull($value){ $this->allow_null = $value; return $this; }

	public function Render(){
		echo new HiddenControl($this->name,$this->value);

		echo '<span style="white-space:nowrap;">';
		echo '<input type="text" name="'.$this->name.'box" id="'.$this->name.'box" value="'.(is_null($this->value)?'':$this->value->format('d/m/Y')).'" class="formLocked" readonly="readonly" style="width:71px;text-align:center;" />';
		echo '<a href="javascript:'.$this->name.'ToggleCalendar();">';
		echo '<img src="oxy/img/arrow_down.gif" alt="" hspace="2" />';
		echo '</a>';
		echo '</span>';

		echo '<div class="calendar" id="'.$this->name.'calendar" style="display:none;z-index:100;"></div>';

		echo Js::BEGIN;
		echo "var ".$this->name."_date=".new Js($this->value).";";

		echo $this->name."ToggleCalendar = function(){";
		echo "var d = ".$this->name."_date;";
		echo "if ($('".$this->name."calendar').style.display!='none')";
		echo "$('".$this->name."calendar').hide();";
		echo "else {";
		echo $this->name.'ShowMonth(d);';
		echo "$('".$this->name."calendar').show();";
		echo "$('".$this->name."calendar').absolutize();";
		echo "$('".$this->name."calendar').style.left=$('".$this->name."box').positionedOffset().left+'px';";
		echo "}";
		echo "};";


		echo $this->name."ShowMonth = function(x){";
		echo "var s = '<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">';";

		echo "var d = ".$this->name."_date;";
		echo "var cm = x==null ? ".new Js(new DateTime())." : x;";
		echo "cm = new Date(cm.getFullYear(),cm.getMonth(),1);";
		echo "var dp = new Date(cm.getFullYear(),cm.getMonth(),1).add({months:-1});";
		echo "var dn = new Date(cm.getFullYear(),cm.getMonth(),1).add({months:1});";

		echo "var months=new Array(".new Js(Lemma::Retrieve('January'))
														.",".new Js(Lemma::Retrieve('February'))
														.",".new Js(Lemma::Retrieve('March'))
														.",".new Js(Lemma::Retrieve('April'))
														.",".new Js(Lemma::Retrieve('May'))
														.",".new Js(Lemma::Retrieve('June'))
														.",".new Js(Lemma::Retrieve('July'))
														.",".new Js(Lemma::Retrieve('August'))
														.",".new Js(Lemma::Retrieve('September'))
														.",".new Js(Lemma::Retrieve('October'))
														.",".new Js(Lemma::Retrieve('November'))
														.",".new Js(Lemma::Retrieve('December'))
														.");";
		echo "s+='<tr>';";
		echo "s+='<td class=\"monthx\"><a href=\"javascript:".$this->name."ShowMonth(new Date('+dp.getFullYear()+','+dp.getMonth()+','+dp.getDate()+'));\">&laquo;</a></td>';";
		echo "s+='<td class=\"month\" colspan=\"5\">'+months[cm.getMonth()]+' '+cm.getFullYear()+'</td>';";
		echo "s+='<td class=\"monthx\"><a href=\"javascript:".$this->name."ShowMonth(new Date('+dn.getFullYear()+','+dn.getMonth()+','+dn.getDate()+'));\">&raquo;</a></td>';";
		echo "s+='</tr>';";

		$days = array();
		echo "var days=new Array(".new Js(substr(Lemma::Retrieve('Monday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Tuesday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Wednesday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Thursday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Friday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Saturday'),0,1))
													.",".new Js(substr(Lemma::Retrieve('Sunday'),0,1))
													.");";

		echo "s+='<tr>';";
		echo "for (i=0;i<days.length;i++)";
		echo "  s+='<td class=\"day\">'+days[i]+'</td>';";
		echo "s+='</tr>';";

		echo "for (i=0; i<cm.getDay(); i++){ if (i==0) s+='<tr>'; s+='<td class=\"empty\">&nbsp;</td>'; }";
		echo "var m = cm.getMonth();";
		echo "for (;m==cm.getMonth();cm.add({days:1})){";
		echo "b=false;";
		echo "if (d!=null) b=cm.getFullYear()==d.getFullYear()&&cm.getMonth()==d.getMonth()&&cm.getDate()==d.getDate();";
		echo "if (cm.getDay()==0) s+='<tr>';";
		echo "s+='<td align=\"center\" class=\"'+(b?'selectedday':(cm.getDay()==0||cm.getDay()==6?'weekend':'weekday'))+'\">';";
		echo "s+='<a href=\"javascript:".$this->name."SetDate(new Date('+cm.getFullYear()+','+cm.getMonth()+','+cm.getDate()+'));\">';";
		echo "s+=cm.getDate();";
		echo "s+='</a>';";
		echo "s+='</td>';";
		echo "if (cm.getDay()==6) s+='</tr>';";
		echo "}";
		echo "if (cm.getDay()!=0) for (i=cm.getDay();i<7;i++){ s+='<td class=\"empty\">&nbsp;</td>'; if (i==6) s+='</tr>'; }";

		if ($this->allow_null){
			echo "b=d==null;";
			echo "s+='<tr><td colspan=\"7\" align=\"center\" class=\"'+(b?'selectedday':'weekend')+'\">';";
			echo "s+='<a href=\"javascript:".$this->name."SetDate(null);\">';";
			echo "s+=".new Js('&empty;').";";
			echo "s+='</a>';";
			echo "s+='</td></tr>';";
		}
		echo "s+='</table>';";


		echo "$('".$this->name."calendar').update(s);";
		echo "};";

		echo $this->name."SetDate = function(x){";
		echo $this->name."_date=x;";
		echo 'if (x==null){';
		echo "$('".$this->name."box').value='';";
		echo "$('".$this->name."').value='';";
		echo '}';
		echo 'else{';
		echo "day=x.getDate(); if (x.getDate()<10) {day='0'+x.getDate();}";
		echo "month=x.getMonth()+1; if ((x.getMonth()+1)<10) {month='0'+(x.getMonth()+1);}";
		echo "year=x.getFullYear();";
		echo "$('".$this->name."box').value=day+'/'+month+'/'+year;";
		echo "$('".$this->name."').value=year+month+day+'000000';";
		echo '}';
		echo "$('".$this->name."calendar').hide();";
		echo "};";


		echo $this->name."getDate = function(strDate){";
		echo "year = strDate.substring(0,4);";
		echo "month = strDate.substring(4,2);";
		echo "day = strDate.substring(6,2);";
		echo "d = new Date();";
		echo "d.setDate(day);";
		echo "d.setMonth(month);";
		echo "d.setFullYear(year);";
		echo "return d; ";
		echo "};";

		echo Js::END;

	}
}



?>
